//
//  Album.m
//  第九节作业
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "Album.h"
#import "MyScroller.h"
@interface Album ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIPageControl *page;
@property (nonatomic, retain) MyScroller *scroll;

@end


@implementation Album
- (void)dealloc {
    
    [_page release];
    [_scroll release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame withImages:(NSArray<UIImage *>    *)images{
    self = [super initWithFrame:frame];
    if (self) {
        [self configWithFrame:frame images:images];
        
    }
    return self;
}

- (void)configWithFrame:(CGRect)frame images:(NSArray *)images{
     self.scroll = [[MyScroller alloc] initWithFrame:self.bounds withImages:images delegate:self];
    [_scroll release];
    self.scroll.delegate = self;
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, frame.size.width - 40, frame.size.width, 40)];
    [self addSubview:self.page];
    [_page release];
    self.page.numberOfPages = images.count;
    [self.page addTarget:self action:@selector(handlePage:) forControlEvents:UIControlEventValueChanged];
    
}

-(void)handlePage:(UIPageControl *)page{
    [self.scroll setContentOffset:CGPointMake(self.bounds.size.width*page.currentPage, 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.page.currentPage = scrollView.contentOffset.x / self.bounds.size.width;
    if (scrollView == self.scroll) {
        
        for (UIScrollView *scrollOfSmall in self.scroll.subviews) {
            
            if ([scrollOfSmall.class isSubclassOfClass:[UIScrollView class]]) {
                
                scrollOfSmall.zoomScale = 1;
            }
            
        }
    }
    

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
    // 判断是小scrollView
    if (scrollView != self.scroll) {
        
        NSLog(@"%@", scrollView);
        return [scrollView.subviews firstObject];
        
    } else {
        
        return nil;
    }
    
}
















/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
